package UCM;

import Source.CreateUser;
import Views.ViewCreateUser;

/**
 * The user case for create user.
 * @author Joshua
 */
public class UCCreateUser implements UCController {

    private ViewCreateUser view;
    private CreateUser model = new CreateUser();

    /**
     * Constructor that initialises the view
     * @param view
     */
    public UCCreateUser(ViewCreateUser view) {
        this.view = view;
        view.loadTo(model);
    }
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.AD87EB5B-A7A0-C362-62A0-6D83116B2F29]
    // </editor-fold> 

    /**
     * Precondition: The user clicks the create account button and create user view is loaded.

    1. User fills out the fields and clicks make account.
    2. System passes entered data to the program.
    3. System determines data is useable.
    4. System commits data.
    5. System logs the user in.
    6. System adds the user to the list of online users.
    7. System returns to the main UI and user is logged in.
    
    Postcondition: User logged in and on the main UI. Account has been created.

    Unsuccessful Outcomes:

    3a) System determines data entered by the user is inappropriate.
    System updates create user UI and explains errors to the User.
    4a) System can't commit data.
    System updates the UI informing the user that the service is currently unavailable.
    4b) System receives notice that an error has occurred.
    System updates the UI informing the user that there was an internal error.
     */
    public void run() {
        //2. System passes entered data to the program.
        view.setModelData();
        //3. System determines data is useable.
        if (model.checkData()) {
            //4. System commits data.
            if (model.commitData()) {
                //5. System logs the user in.
                model.login();
                //6. System adds the user to the list of online users.
                //7. System returns to the main UI and user is logged in.
                view.updateLogin();
            } else {
                /*4a) System can't commit data.
                System updates the UI informing the user that the service is currently unavailable.
                4b) System receives notice that an error has occurred.
                System updates the UI informing the user that there was an internal error. 
                 */
            }
        } else {
            //3a) System determines data entered by the user is inappropriate.
            //System updates create user UI and explains errors to the User.
            view.updateIncorrectInput();
        }
    }
}

